package cn.wsn2018.tools.dao.impl;

import cn.wsn2018.tools.dao.EncodeDesignDao;
import cn.wsn2018.tools.model.Design.EncodeDesign;
import net.sf.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;
import org.springframework.stereotype.Repository;

/**
 * Created by NZX on 2019/7/27.
 */
@Repository("encodeDesignDao")
public class EncodeDesignDaoImpl implements EncodeDesignDao{

    @Autowired
    private MongoTemplate mongoTemplate;

    @Override
    public String saveEncodeDesign(EncodeDesign encodeDesign) {
        JSONObject result = JSONObject.fromObject(mongoTemplate.insert(encodeDesign,"encodeDesign"));
        String id = result.optString("id");
        if(id!=null&&!"".equals(id)) {
            return id;
        }
        else {
            return null;
        }
    }

    @Override
    public EncodeDesign getEncodeDesign(String id) {
        Query query = new Query();
        query.addCriteria(Criteria.where("_id").is(id));
        EncodeDesign encodeDesign = mongoTemplate.findOne(query,EncodeDesign.class,"encodeDesign");
        return encodeDesign;
    }

    @Override
    public EncodeDesign getEncodeDesignByDesignId(String designId) {
        Query query = new Query();
        query.addCriteria(Criteria.where("designId").is(designId));
        EncodeDesign encodeDesign = mongoTemplate.findOne(query,EncodeDesign.class,"encodeDesign");
        return encodeDesign;
    }

    @Override
    public boolean updateEncodeDesign(EncodeDesign encodeDesign) {
        Query query = new Query();
        query.addCriteria(Criteria.where("designId").is(encodeDesign.getDesignId()));
        Update update = new Update();
        if(encodeDesign.getEncryptFunc()!=null){
            update.set("encryptFunc",encodeDesign.getEncryptFunc());
        }
        if(encodeDesign.getIntegrityFunc()!=null){
            update.set("integrityFunc",encodeDesign.getIntegrityFunc());
        }
        if(encodeDesign.getExceptionFunc()!=null){
            update.set("exceptionFunc",encodeDesign.getExceptionFunc());
        }
        if(encodeDesign.getEncryptOutput()!=null){
            update.set("encryptOutput",encodeDesign.getEncryptOutput());
        }
        if(encodeDesign.getIntegrityResArea()!=null){
            update.set("integrityResArea",encodeDesign.getIntegrityResArea());
        }
        if(encodeDesign.getExceptionRes()!=null){
            update.set("exceptionRes",encodeDesign.getExceptionRes());
        }
        JSONObject result = JSONObject.fromObject(mongoTemplate.updateFirst(query,update,"encodeDesign"));
        String count = result.optString("modifiedCount");
        if(count!=null&&"1".equals(count)){
            return true;
        }else {
            return false;
        }
    }

    @Override
    public boolean deleteEncodeDesign(String designId) {
        Query query = new Query();
        query.addCriteria(Criteria.where("designId").is(designId));
        JSONObject result = JSONObject.fromObject(mongoTemplate.remove(query,"encodeDesign"));
        String count = result.optString("deletedCount");
        if(count!=null&&"1".equals(count)){
            return true;
        }else {
            return false;
        }
    }
}
